!pr0
!lm12
!rm75
Speeding Up Spirals........................Bob Sander-Cederlof

Several have written to us about Roger Keating's Spiral Screen Clear (AAL June 1983).  Charles Putney, who you may remember as the first one to double the speed of the prime number program in AAL several years ago, has now applied his talent to unwinding the screen.

Roger's program ran in 55 seconds, my table-lookup for BASCALC shortened it to 40 seconds.  Charlie wrote the whole thing out as one long string of LDA-STA pairs, and trimmed the time to only 7 seconds!

Let's see...there are 960 characters on the screen.  If I write a LDA-STA pair to move each byte ahead one position along the spiral path, I will have 959 such pairs.  Each LDA and each STA will take 3 bytes, so the program to shift the whole screen one step around the spiral path will take 2x3x959 = 5754 bytes.  Add another 5 bytes to LDA #$A0 and store it in the center of the screen before the first rotation. Then add some code to re- run the 959 steps 959 more times, so that the whole screen clears, and you get Charlie's program, 5777 bytes.

Now try to type it all in!  Don't worry, we aren't even going to list it here.  It will be on the next Quarterly disk, though.

Charlie decided to use five macros, to decrease the amount of manual labor involved.  He defined a macro named MOVE which builds the LDA-STA pair for a pair of arguments:

       .MA MOVE
       LDA ]1
       STA ]2
       .EM

Then he defined one macro for each leg of the spiral:  MOVED, MOVEL, MOVEU, and MOVER for down, left, up, and right respec- tively.  With a few comment lines, the macro definitions take a mere 488 lines!  The macros are each called with three parameters:

       >MOVED col,low.row,high.row
       >MOVEL row,low.col,high.col
       >MOVEU col,low.row,high.row
       >MOVER row,low.col,high.col

The definitions out of the way, it only remains to write 12 sets of 4 macro calls, or 48 lines, and a driving loop to do it all 960 times.  Here is a condensed listing of the actual code part of Charlie's program:
!np
!psCHANGE TO ELITE TYPE AND SPACING
!lm+5
6400 *--------------------------------
6410 *
6420 *      SPIRAL PROGRAM
6430        .OR $6000    OUT OF THE WAY
6440        .TF SPIRAL.OBJ
6450 *
6460 *
6470 SPIRAL LDA #' '+$80 GET A SPACE
6480        STA R12+12   PUT IT IN CENTER
6490        LDX #960     HOW MANY TIMES ?
6500        LDY /960     HIGH ORDER
6510 *
6520 SPI1   >MOVED 0,0,23
6530        >MOVEL R0,0,39
6540        >MOVEU 39,0,23
6550        >MOVER R23,1,39
6560 *
6570        >MOVED 1,1,23
6580        >MOVEL R1,1,38
6590        >MOVEU 38,1,22
6600        >MOVER R22,2,38
6610 *
6620        >MOVED 2,2,22
6630        >MOVEL R2,2,37
6640        >MOVEU 37,2,21
6650        >MOVER R21,3,37
6660 *
6670        >MOVED 3,3,21
6680        >MOVEL R3,3,36
6690        >MOVEU 36,3,20
6700        >MOVER R20,4,36
6710 *
6720        >MOVED 4,4,20
6730        >MOVEL R4,4,35
6740        >MOVEU 35,4,19
6750        >MOVER R19,5,35
6760 *
6770        >MOVED 5,5,19
6780        >MOVEL R5,5,34
6790        >MOVEU 34,5,18
6800        >MOVER R18,6,34
6810 *
6820        >MOVED 6,6,18
6830        >MOVEL R6,6,33
6840        >MOVEU 33,6,17
6850        >MOVER R17,7,33
6860 *
6870        >MOVED 7,7,17
6880        >MOVEL R7,7,32
6890        >MOVEU 32,7,16
6900        >MOVER R16,8,32
6910 *
6920        >MOVED 8,8,16
6930        >MOVEL R8,8,31
6940        >MOVEU 31,8,15
6950        >MOVER R15,9,31
6960 *
6970        >MOVED 9,9,15
6980        >MOVEL R9,9,30
6990        >MOVEU 30,9,14
7000        >MOVER R14,10,30
7010 *
7020        >MOVED 10,10,14
7030        >MOVEL R10,10,29
7040        >MOVEU 29,10,13
7050        >MOVER R13,11,29
7060 *
7070        >MOVED 11,11,13
7080        >MOVEL R11,11,28
7090        >MOVEU 28,11,12
7100        >MOVER R12,12,28
7110 *
7120        DEX
7130        CPX #$FF
7140        BNE SPI2
7150        DEY
7160        CPY #$FF
7170        BNE SPI2
7180        RTS
7190 SPI2   JMP SPI1
!lm-5
!np
!psNOW CHANGE BACK TO PICA
Remember, the whole source with the full macro definitions will be on the next quarterly disk ($15, for all source code in issues July-August-September 1983).

Because Charlie's program makes such heavy use of macros, it takes considerable time to assemble.  He timed it at nearly two minutes.  If the program were written out the long way, without macros, it would take only about 20 seconds to assemble.

Charlie pointed out that we are needlessly moving the center of the spiral, which is already blank.  As the blanked portion grows, this becomes very significant.  In fact, by eliminating moving the cleared portion, the time could be further reduced to only 3 1/2 seconds.  Each LDA-STA takes 8 cycles.  The long way takes 959*960 pairs, plus some overhead.  Ignoring the overhead, we get 7365120 cycles, or about 7.2 seconds.  Forgetting the blanked stuff makes it 3.6 seconds.  Any takers?

And I was just wondering...how about an Applesoft program which writes the 959 LDA-STA pairs as assembly language source on a text file?  Or POKEs the actual object code, by computing the addresses necessary, into a binary buffer area.  Again, any takers?
